<%@ page language="java" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
${aier_doctype}
<html <s:property value="aier-html-xmlns" escape="false"/>>
<head>
<meta http-equiv="Content-Type" content="${aier_content_type}" />
<meta name="keywords" content="${aier_keyworks}" />
<meta name="description" content="${aier_description}" />
<title>${aier_title}</title>
<link rel="stylesheet" href="${aier_css_url}${aier_jqui_css}ui.css" />
<link rel="Stylesheet" href="${aier_css_url}jq/jq.css" />
<link rel="Stylesheet" href="${aier_css_url}jq/multiselect.css" />
<link rel="stylesheet" href="${aier_css_url}jc/chosen.css" >
<link rel="stylesheet" href="${aier_css_url}social/module/jq-msg.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/reset.css" />
<link rel="stylesheet" href="${aier_css_url}social/school/smanage.css" />

<script type="text/javascript" src="${aier_js_url}j.js"></script>
<script type="text/javascript" src="${aier_js_url}b.js"></script>
<script type="text/javascript" src="${aier_js_url}${aier_jqui_js}ui.js"></script>
<script type="text/javascript" src="${aier_js_url}97/WdatePicker.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/msg.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/cn.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/jq.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/ja.js"></script>
<script type="text/javascript" src="${aier_js_url}jc/cj.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/block.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/multiselect.js"></script>
<script type="text/javascript" src="${aier_js_url}social/school/steacher.js"></script>
<script type="text/javascript">
	var global_suffix = "${aier_suffix}", global_url = "${aier_url}", global_uri = "${aier_uri}", global_urijs = "${aier_urijs}",
	global_sid = "${sid}",global_tid = "${tid}";
</script>
</head>
<!-- 教师管理页面 -->
<body>

	<!-- 顶栏 -->
	<s:action name="userhomecomm_homeHead" namespace="/user"
		executeResult="true"></s:action>

	<!-- 正文 -->
	<div id="hfContent" class="ar-content">
		<div class="content-main">
			<!-- 学校名称栏 -->
			<s:action name="scomm_schoolMTitle" namespace="/school" executeResult="true"></s:action>
			<div class="main-content wrap-bg clearfix">
				<!-- 左侧栏 -->
				<s:action name="scomm_schoolMLeft" namespace="/school" executeResult="true"></s:action>
				<!-- 右侧内容 -->
				<div class="ae-right">
					<div class="right-main">
						<div class="right-main-top">
							<div class="binfo-title">
								<span>教师管理</span>
							</div>
							<div class="right-line"></div>
							
						</div>
						<div class="right-main-c">
							<div id="tabsCm">
							    <ul>
                                    <li><a href="#tabs-1">教师列表</a>
                                    </li>
                                    <li><a href="#tabs-2" onclick="showCtInviteInfo()">添加教师</a> 
                                    </li>
                                </ul>
                                <div id="tabs-1">
									<table style="margin-left: 50px;">
	
										<tr>
											<th class="gray6" style="*width: 150px;">姓名与账户：</th>
											<td >
												<s:select id="selTname" list="tnameAccountList"
														data-placeholder="请选择或输入姓名" cssClass="chosen" tabindex="4">
												</s:select>
											</td>
											<th class="gray6">职务：</th>
											<td >
												<select id="selSgrade" class="chosen">
													<option value="-1">所有职务</option>
													<c:forEach var="sg" items="${schoolGradeList }">
														<option value="${sg.gid}">${sg.name}</option>
													</c:forEach>
												</select>
											</td>
											<td style="width: 150px;" align="right">
												<a id="stJurBtn" href="javascript:;"
												class="cm-search-btn"></a>
											</td>
										</tr>
									</table>
									<div class="right-main-down">
			                            <div class="down-inre">
			                                <table id="sTeachersInfo"></table>
			                                <div id="sTeachersPage"></div>
			                            </div>
			                        </div>
								</div>
								<div id="tabs-2">
                                    <p class="prompt">请填写相关资料，添加教师一起参与班级互动吧！</p>
                                    <div class="info-tab01">
                                        <table id="inviteTeaTable">
                                            <thead>
                                                <tr>
                                                    <th class="gray6">班级</th>
                                                    <th class="gray6">教师姓名</th>
                                                    <th class="gray6">职务</th>
                                                    <th class="gray6">手机号码</th>
                                                    <th>&nbsp;</th>
                                                    <th>&nbsp;</th>
                                                </tr>
                                            </thead>

                                            <tbody>
                                                <tr>
                                                    <td><select id="cTeaClaSel_0" data-placeholder="请选择班级"
                                                        Class="chosen" tabindex="1" style="width:134px;">
                                                            <option value="-1">请选择</option>
                                                            <c:forEach var="cls" items="${classList }">
								                                <option value="${cls.cid}">${cls.cname}</option>
								                            </c:forEach>
                                                    </select></td>
                                                    <td><input type="text" id="cTeaName_0"
                                                        placeholder="请输入教师名">
                                                    </td>
                                                    <td><select id="cTeaGraSel_0" data-placeholder="请选择职务"
                                                        Class="chosen" tabindex="1" style="width:134px;">
                                                            <option value="-1">请选择</option>
                                                            <c:forEach var="sg" items="${schoolGradeList }">
                                                                <option value="${sg.gid}">${sg.name}</option>
                                                            </c:forEach>
                                                    </select></td>
                                                    <td><input type="text" id="cTeaPhone_0"
                                                        placeholder="请输入手机号">
                                                    </td>
                                                    <td><a id="inTeaDelBtn" href="javascript:;"
                                                        class="creare-stu-btn"><em>删除</em> </a>
                                                    </td>
                                                    <td><a id="inTeaAddBtn" href="javascript:;"
                                                        class="creare-stu-btn"><em>新增</em> </a>
                                                    </td>
                                                </tr>
                                            </tbody>

                                        </table>
                                    </div>
                                    <div class="cpa-invite-btn">
                                        <a id="cteInviteBtn" href="javascript:;"
                                            class="creare-stu-btn" onclick="invite_teacher();"><em>添加</em> </a>
                                    </div>
                                    <div class="right-main-down">
                                        <div class="binfo-title">
                                            <h4>添加记录</h4>
                                        </div>
                                        <div class="down-line"></div>

                                        <div class="down-inre">
                                            <table id="ctInviteInfo"></table>
                                            <div id="ctInvitePage"></div>
                                        </div>
                                    </div>
                                </div>
							</div>
						</div>
					</div>
				</div>

			</div>

		</div>
		<!-- footer copyright -->
		<s:action name="userhomecomm_homeFooter" namespace="/user" executeResult="true"></s:action>
		<!-- /footer copyright -->
	</div>

	<!-- 教师管理弹出层 -->
	<div id="stJurDiv" title="教师权限管理" style="display: none;margin-top: 30px;">
		<div class="info-tab01">
			<table style="width:460px;">
				<tr>
					<th class="gray6">职务：</th>
					<td>
						<select id="upSgrade" class="chosen">
							<option value="-1">请选择</option>
							<c:forEach var="sg" items="${schoolGradeList }">
								<option value="${sg.gid}">${sg.name}</option>
							</c:forEach>
						</select>
					</td>
				</tr>
				<tr>
					<th class="gray6">所在班级：</th>
					<td style="width: 260px;">
						<select id="tInClass" style="width:188px;" multiple="multiple">
							<c:forEach var="cls" items="${classList }">
								<option value="${cls.cid}">${cls.cname}</option>
							</c:forEach>
						</select>
					</td>
				</tr>
				<tr>
					<th class="gray6">管理班级：</th>
					<td style="width: 260px;">
						<select id="tMclass" style="width:188px;" multiple="multiple">
							<c:forEach var="cls" items="${classList }">
								<option value="${cls.cid}">${cls.cname}</option>
							</c:forEach>
						</select>
					</td>
				</tr>

			</table>
		</div>
	</div>
	
	<!-- 删除弹出层 -->
	<div id="compPrompt" title="提示" style="display: none;">
		<span>确认删除吗？</span>
	</div>
</body>
<script type="text/javascript">
    $("#tabsCm").tabs();
	$(".chosen").chosen();
	//设置班级为下拉复选框
	$("#tInClass").multiselect({
		noneSelectedText : "请选择",
		selectedText : "已选中#个班级",
		selectedList : 2
	});
	$("#tMclass").multiselect({
		noneSelectedText : "请选择",
		selectedText : "已选中#个班级",
		selectedList : 2
	});
	
	//管理
	function upTel(id){
	//添加班级弹出层
		$("#stJurDiv").dialog({
			//背景变暗
			modal: true,
			autoOpen : false,
			show : "fade",
	        hide : "fade",
			width : "570",
			height : "330",
			buttons: {
				"确定": function() {			
					var cidList = $('#tInClass').val();
					var cidListNew = "";
					if(cidList!=null){
						var cidListNew = cidList.toString();
					}
					var cidList_isadmin = $('#tMclass').val();
					var cidList_isadminNew = "";
					if(cidList_isadmin!=null){
						var cidList_isadminNew = cidList_isadmin.toString();
					}
					if(checkClassList(cidListNew,cidList_isadminNew)=="false"){
						$().message("管理班级必须在 所在班级里");
						return
					};
					
					var funadd = function(data){
						if (data.s == 1) {
							$().message("修改教师成功!");
							$("#sTeachersInfo").trigger("reloadGrid");
						} else {
							$().message(data.error_info);
						}
					};
					exeAjax(ajaxUrl("${aier_url}${aier_urijs}schoolteachers_updateSchoolTeacherManager${aier_suffix}"), funadd, {
					"tid" :jQuery("#sTeachersInfo").jqGrid('getRowData', id).tid,
					"gid":$('#upSgrade  option:selected').val(),
					"cidList":cidListNew,
					"cidList_isadmin":cidList_isadminNew});
				},"取消": function() {
					$( this ).dialog( "close" );
				}
			}
		});
		$("#stJurDiv").dialog("open");
		initSchoolTeacher(id);
	}
	
	//查询
	$("#stJurBtn").click(function() {
		var cid = $('#classid  option:selected').val();
		var nameAndAccount = $("#selTname").val();
		if(nameAndAccount=="所有教师"){
			var account = "";	
		}else{
			var account = nameAndAccount.split(" ")[1];
		}
		var gid = $('#selSgrade  option:selected').val();
		jQuery("#sTeachersInfo").jqGrid('setGridParam',{url:"${aier_url}${aier_urijs}schoolteachers_searchSchoolTeacherList${aier_suffix}?sid="+global_sid
			+"&account="+account+"&gid="+gid,page:1}).trigger("reloadGrid");
		return false;
	});
	
	//加载teacher的所在班级和管理班级 tInClass tMclass
	function initSchoolTeacher(id){
		var funadd = function(data){
			var classList = data.schoolTeacher.classTeacherList;
			$("#tInClass").attr("value","");//清空选中项。
			if(classList!=null){ 
				var count="${classList.size()}";//获取下拉框的长度。 
				for(var c=0;c<classList.length;c++){ 
					for(var c_i=0;c_i<count;c_i++) { 
						if($("#tInClass").get(0).options[c_i].value == classList[c].cid) { 
							$("#tInClass").get(0).options[c_i].selected = true;//所在班级设置为选中。
							if(classList[c].isadmin==1){
								$("#tMclass").get(0).options[c_i].selected = true;//管理班级设置为选中。
							}
						} 
					} 
				} 			
			}
			$("#tInClass").multiselect('refresh');	
			$("#tMclass").multiselect('refresh');
			if(data.schoolTeacher.gid==null){
				$('#upSgrade').val("-1");
			}else{
				$('#upSgrade').val(data.schoolTeacher.gid);
			}
			
		};
		exeAjax(ajaxUrl("${aier_url}${aier_urijs}schoolteachers_showSchoolTeacher${aier_suffix}"), funadd, {
			"tid" :jQuery("#sTeachersInfo").jqGrid('getRowData', id).tid});
	}
	
	
	//检查 管理班级必须在 所在班级里
	function checkClassList(cidtmp,cidtmp_isadmin){
		if(cidtmp!=""&&cidtmp_isadmin!=""){
			var cidList = cidtmp.split(",");
			var cidList_isadmin = cidtmp_isadmin.split(",");
			for(var c=0;c<cidList_isadmin.length;c++){
				var flag = false;
				var cid_ma = cidList_isadmin[c];
				for(var j=0;j<cidList.length;j++){
					var cid_in = cidList[j];
					if(cid_ma==cid_in){
						flag = true;
						continue;
					}
				}
				if(flag){
					continue;
				}else{
					return "false";
				}
			}
		}
		if(cidtmp==""&&cidtmp_isadmin!=""){
			return "false";
		}
		return "true";
	}
	
	var tid = 0;
    var tidList = "0,";
	//增加教师<tr/>
    $("#inTeaAddBtn")
            .click(
                    function() {
                        tid = tid + 1;
                        tidList = tidList + tid + ",";
                        $("#inviteTeaTable tbody")
                                .append(
                                        "<tr id='cimitr_t_"+tid+"'><td><select id='cTeaClaSel_"+tid+"' data-placeholder='请选择班级' Class='chosen' tabindex='1' style='width:134px;'>"
                                        +"<option value='-1'>请选择</option><c:forEach var='cls' items='${classList }'><option value='${cls.cid}'>${cls.cname}</option></c:forEach></select></td><td><input type='text' id='cTeaName_"+tid
                                        +"' placeholder='请输入教师名'></td><td><select id='cTeaGraSel_"+tid  
                                        +"' data-placeholder='请选择职务' Class='chosen' tabindex='1' style='width:134px;'><option value='-1'>请选择</option><c:forEach var='sg' items='${schoolGradeList }'><option value='${sg.gid}'>${sg.name}</option></c:forEach></select></td><td><input type='text' id='cTeaPhone_"+tid
                                        +"' placeholder='请输入手机号'></td><td><a id='inTeaDelBtn' href='javascript:;' class='creare-stu-btn' onclick='deltr_t("+ tid
                                        + ")'><em>删除</em> </a></td><td></td></tr>");
                        $(".chosen").chosen();
                    });
    //删除教师<tr/>
    function deltr_t(index) {
        $("#cimitr_t_" + index).remove();//删除当前行
        tidList = tidList.replace(","+index+",", ",");

    };
    
  //邀请教师
    function invite_teacher(){
        var inviteTea_message = "";
        var idList = tidList.split(",");//分割为Ojbect数组。 
        for(var i=0;i<idList.length;i++){ 
            var index = idList[i];
            if(index!=""){
                //取数据 tname,gid,phone
                var cid = $("#cTeaClaSel_"+index+" option:selected").val();
                var tname = $("#cTeaName_"+index).val().trim();
                var gid = $("#cTeaGraSel_"+index+" option:selected").val();
                var phone = $("#cTeaPhone_"+index).val().trim();
                if(tname==""){
                    $().message("请输入教师名");
                    return 
                }
                if(phone==""){
                    $().message("请输入手机号");
                    return 
                }
                if(!check_mobile(phone)){
                    $().message("手机号:" + phone + "格式有误");
                    return 
                }
                inviteTea_message = inviteTea_message + cid + "," + tname + "," + gid + "," + phone + "@";
            }
        }
        var funadd = function(data){
            if (data.s == 1) {
                $().message("添加成功!");
                $("#ctInviteInfo").trigger("reloadGrid");
                for (;tid>0;tid--) {
                	$("#cimitr_t_" + tid).remove();
                }
                tidList = "0,";
                $("#cTeaName_0").val("");
                $("#cTeaPhone_0").val("");
            } else {
                $().message(data.error_info);
            }
        };
        exeAjax(ajaxUrl("${aier_url}${aier_urijs}schoolteachers_addTeacherInvite${aier_suffix}"), funadd, {
        	"sid":global_sid,
            "tid":global_tid,
            "inviteTea_message":inviteTea_message});    
    }
</script>
</html>